<?php
namespace app\classroom\controller;
use app\classroom\model\ExcelModel;
use cmf\controller\AdminBaseController;
//require('E:\xampp\htdocs\xueyue\trunk\simplewind\extend\phpexcel\PHPExcel.php');

class ClassroomTemplateController extends  AdminBaseController
{
	public  function  index2()
	{
		import('phpexcel.PHPExcel',EXTEND_PATH);
		$model=new ExcelModel();
		$model=$model->index();
		$objPHPExecel=new \PHPExcel();
		for($i=0;$i<2;$i++)
		{
			if($i>1)
			{
				$objPHPExecel->createSheet();//创建的内置方法
			}
			$objPHPExecel->setActiveSheetIndex($i-1);
			$objSheet=$objPHPExecel->getActiveSheet();//获取当前活动sheet
			$objSheet->setCellValue('A1','姓名')->setCellValue('1','班级')->setCellValue('B1','性别');
			$j=2;
			foreach($model as $k=>$v)
			{
				$objSheet->setCellValue('A'.$j,$v['id'])->setCellValue('B'.$j,$v['code'])->setCellValue('C'.$j,$v['count']);
				$j++;
			}
		}
		$objWrite=\PHPExcel_IOFactory::createWriter($objPHPExecel,'Excel2007');
		$dir=dirname(__FILE__);
		$objWrite->save($dir.'testexcel11.xlsx');
	}
	//index
	public  function  index()
	{
		import('phpexcel.PHPExcel',EXTEND_PATH);
		 $model= new ExcelModel();
		$data=$model->index();
		$objPHPExcel=new \PHPExcel();
		$objSheet=$objPHPExcel->getActiveSheet(); //获取当前活动sheet的操作对象
		$objSheet->setTitle('demo');
		$objSheet->setCellValue('A1','姓名')->setCellValue('B1','分数')->setCellValue('C1','分数');
		$j=2;
		foreach($data as $k=>$v)
		{
			$objSheet->setCellValue('A'.$j,$v['id'])->setCellValue('B'.$j,$v['id'])->setCellValue('C'.$j,$v['id']);
			$j++;
		}
		$objWrite=\PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
		$dir=dirname(__FILE__);
		var_dump($dir);
//		$objWrite->save($dir.'testexcel111.xlsx');

		$this->browser_export('Excel5','browser_excel03.xls');//输出到浏览器
		$objWrite->save("php://output");

		return $this->fetch();

	}
	function browser_export($type,$filename){
		if($type=="Excel5"){
			header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件
		}else{
			header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器数据excel07文件
		}
		header('Content-Disposition: attachment;filename="'.$filename.'"');//告诉浏览器将输出文件的名称
		header('Cache-Control: max-age=0');//禁止缓存
	}
	//刚刚写完的好用的excel
    public  function  index1()
    {
		import('phpexcel.PHPExcel',EXTEND_PATH);
        $objPHPExcel=new \PHPExcel();
        $objSheet=$objPHPExcel->getActiveSheet(); //获取当前活动sheet的操作对象
        $objSheet->setTitle('demo');
       /** $objSheet->setCellValue('A1','姓名')->setCellValue('B1','分数');
        $objSheet->setCellValue('A2','张三')->setCellValue('B2','50');**/
		//可以是数组
		$arr=array(
			array(),
			array("",'姓名','分数'),
			array('','李四','60'),
			array('','王五',70)
		);
		$objSheet->fromArray($arr);
        $objWrite=\PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
        $dir=dirname(__FILE__);
        var_dump($dir);
        $objWrite->save($dir.'testexcel1.xlsx');


       return $this->fetch();
    }
    //添加教室位置模板管理的 页面
  public function add()
  {
      return $this->fetch();
  }
    //教师位置模板的保存成功
    public function save()
    {

    }

}